package com.ugrow.ugrowcommunity.activity.professional;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;

import com.ugrow.ugrowcommunity.R;
import com.ugrow.ugrowcommunity.activity.announcement.vm.ActivitysFragment;
import com.ugrow.ugrowcommunity.activity.announcement.vm.EducationFragment;
import com.ugrow.ugrowcommunity.activity.announcement.vm.LogisticsFragment;
import com.ugrow.ugrowcommunity.activity.professional.fragment.BusinessFragment;
import com.ugrow.ugrowcommunity.activity.professional.fragment.EngineeringFragment;
import com.ugrow.ugrowcommunity.data.Data;
import com.ugrow.ugrowcommunity.databinding.ActivityCourseEvaluationBinding;
import com.ugrow.ugrowcommunity.databinding.ActivityCourseSelectionBinding;
import com.ugrow.ugrowcommunity.databinding.ActivityProfessionalBinding;

public class ProfessionalActivity extends AppCompatActivity {

    private ActivityProfessionalBinding binding;

    private BusinessFragment businessFragment;
    private EngineeringFragment engineeringFragment;
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_professional);
        getWindow().setStatusBarColor(0xffffff);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        binding = ActivityProfessionalBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        //初始化基本信息
        binding.name.setText(Data.StudentBean.getData().get(0).getStuName());
        binding.major.setText(Data.StudentBean.getData().get(0).getMajor());
        binding.number.setText(Data.StudentBean.getData().get(0).getStuIdCard());
        binding.college.setText(Data.StudentBean.getData().get(0).getFaculty());
        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


        initFragment();
        addFragment(businessFragment,engineeringFragment);
        hideFragment(engineeringFragment);
        showFragment(businessFragment);
        cheakRadio();
    }

    //初始化Fragment
    private void initFragment(){
        businessFragment = new BusinessFragment();
        engineeringFragment = new EngineeringFragment();
    }

    private void cheakRadio(){
        binding.grop.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.bt_education:
                        hideFragment(engineeringFragment);
                        showFragment(businessFragment);
                        break;
                    case R.id.bt_activitys:
                        hideFragment(businessFragment);
                        showFragment(engineeringFragment);
                        break;
                }
            }
        });
    }

    //一次性添加全部Fragment
    private void addFragment(Fragment... fragments){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        for(Fragment fragment : fragments){
            transaction.add(R.id.fram,fragment);
        }
        //提交事务
        transaction.commit();
    }

    //设置隐藏
    private  void hideFragment(Fragment... fragments){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        for(Fragment fragment : fragments){
            transaction.hide(fragment);
        }
        transaction.commit();
    }

    //设置显示
    private void showFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.show(fragment);
        transaction.commit();
    }
}